package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 根据id查询专辑详情
     *
     * @param albumId
     * @return
     */
    @Override
    public Map<String, Object> getItemById(Long albumId) {
        //result.put("albumInfo", albumInfo);			获取专辑信息
        //result.put("albumStatVo", albumStatVo);		获取专辑统计信息
        //result.put("baseCategoryView", baseCategoryView);	获取分类信息
        //result.put("announcer", userInfoVo);	获取主播信息
        Map<String, Object> result = new HashMap<>();
//        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
//        if (!bloomFilter.contains(albumId)) {
//            log.error("布隆过滤器未命中:{}",albumId);
//            return result;
//        }
        //获取专辑信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //1. 通过专辑Id 获取专辑数据{已存在}
            Result<AlbumInfo> albumInfoReault = albumInfoFeignClient.getAlbumInfo(albumId);
            //判断
            Assert.notNull(albumInfoReault, "专辑信息结果不存在");
            AlbumInfo albumInfo = albumInfoReault.getData();
            //判断
            Assert.notNull(albumInfo, "专辑信息不存在");
            result.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolExecutor);

        //获取专辑统计信息
        CompletableFuture<Void> statCompletableFuture = CompletableFuture.runAsync(() -> {
            Result<AlbumStatVo> statVoResult = albumInfoFeignClient.getStat(albumId);
            //判断
            Assert.notNull(statVoResult, "专辑统计信息结果不存在");
            AlbumStatVo albumStatVo = statVoResult.getData();
            //判断
            Assert.notNull(albumStatVo, "专辑统计信息不存在");
            result.put("albumStatVo", albumStatVo);
        }, threadPoolExecutor);

        //获取分类信息
        CompletableFuture<Void> categoryCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<BaseCategoryView> categoryViewResult = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            //判断
            Assert.notNull(categoryViewResult, "分类信息结果不存在");
            BaseCategoryView baseCategoryView = categoryViewResult.getData();
            //判断
            Assert.notNull(baseCategoryView, "分类信息不存在");
            result.put("baseCategoryView", baseCategoryView);

        }, threadPoolExecutor);

        //获取主播信息
        CompletableFuture<Void> userCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfoVo(albumInfo.getUserId());
            //判断
            Assert.notNull(userInfoVoResult, "主播信息结果不存在");
            UserInfoVo userInfoVo = userInfoVoResult.getData();
            //判断
            Assert.notNull(userInfoVo, "主播信息不存在");
            result.put("announcer", userInfoVo);
        });

        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                statCompletableFuture,
                categoryCompletableFuture,
                userCompletableFuture
        ).join();
        return result;
    }
}
